package cn.wyl.order.service.impl;

import cn.wyl.basic.util.LoginContext;
import cn.wyl.basic.util.PageList;
import cn.wyl.order.domain.OrderPetAcquisition;
import cn.wyl.order.mapper.OrderPetAcquisitionMapper;
import cn.wyl.order.query.OrderPetAcquisitionQuery;
import cn.wyl.order.service.IOrderPetAcquisitionService;
import cn.wyl.basic.service.impl.BaseServiceImpl;
import cn.wyl.order.service.delay.Consumer;
import cn.wyl.order.service.delay.Producer;
import cn.wyl.org.domain.Employee;
import cn.wyl.org.domain.Shop;
import cn.wyl.org.mapper.EmployeeMapper;
import cn.wyl.org.mapper.ShopMapper;
import cn.wyl.pet.domain.Pet;
import cn.wyl.pet.domain.SearchMasterMsg;
import cn.wyl.pet.mapper.PetMapper;
import cn.wyl.pet.mapper.SearchMasterMsgMapper;
import cn.wyl.user.domain.Logininfo;
import org.apache.rocketmq.client.exception.MQClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        // 判断登录员工是那种角色
        //1.employee中shop_id为null，平台管理员
        Long shop_id = employee.getShop_id();
        //2.店铺人员
        if (shop_id!=null){
            Shop shop = shopMapper.loadById(shop_id);
            if (!shop.getAdmin_id().equals(employee.getId())){
                //3.否则就为店员
                query.setEmpId(employee.getId());
            }
            //4.employee中shop_id不为null，通过shop_id查询店铺得到admin_id如果为当前登录管理员的id，就为店铺管理员
            query.setShopId(shop_id);
        }
        //平台管理员 query的EmpId ShopId都为null
        return queryPage(query);
    }

    @Override
    @Transactional
    public void confirm(Map<String, Object> paramsMap) {
        Long orderId = Long.parseLong(paramsMap.get("id").toString());
        Integer payType = Integer.parseInt(paramsMap.get("payType").toString());
        BigDecimal money=new BigDecimal(paramsMap.get("money").toString());
        //1.修改订单：state = 1，money，payType
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderId);
        order.setState(1);
        order.setPrice(money);
        order.setPaytype(payType);
        orderPetAcquisitionMapper.update(order);

        //2.修改寻主消息：state=2，note
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        msg.setState(2);
        msg.setNote("处理完成，交易成功");
        searchMasterMsgMapper.update(msg);

        //修改宠物pet costprice
        Pet pet = petMapper.loadById(order.getPetId());
        pet.setCostprice(money);
        petMapper.update(pet);

    }

    /*取消订单
     * 修改收购订单 state-1 pet_id null
            * 将寻主消息中的shop_id置空(放入寻主池)
     * 删除宠物 - 没有成功收购*/
    @Override
    public void cancelOrder(Long id) {

        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //删除宠物
        petMapper.remove(order.getPetId());
        //修改收购订单
        order.setState(-1);
        order.setPetId(null);
        orderPetAcquisitionMapper.update(order);
        //修改寻主消息
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());



    }


}
